import _ from 'lodash'
function setdisabled(array) {
    if (!Array.isArray(array)) return []
    const recursive = (array) => {
        console.log('array :>> ', array);
        return array.map(v => {
            v.disabled = true
            const child = v.children;
            if (child && child.length) recursive(child)
        })
    }
    recursive(array)
}
export const arrayTreeAddLevel = (array, roleId) => {
    if (!Array.isArray(array)) return []
    const recursive = (array, level = 0) => {
        level++
        return array.map(v => {
            v.level = level
            // v.disabled = v.id == 10010
            const child = v.children;
            // 设置租户管理不可选中
            if (v.id == 10010 && roleId !== 1) {
                v.disabled = v.id == 10010
                setdisabled(child)
            }
            if (child && !v.children.every(e => e.checked)) {
                v.checked = undefined
            } else {
                if (!v.checked) {
                    v.checked = undefined
                }
                // v.checked = undefined
            }
            if (child && child.length) recursive(child, level)
            return v
        })
    }
    return recursive(array)
}


export function buildTree(array, id, parent_id) {
    console.time('123');
    // 创建临时对象
    let temp = {};
    // 创建需要返回的树形对象
    let tree = {};
    // 先遍历数组，将数组的每一项添加到temp对象中
    for (let i in array) {
        temp[array[i][id]] = array[i];
    }

    /*  上面也可以写成
    for(let i=0;i<array.length;i++) {
        temp[array[i][id]] = array[i];
    }
    */

    // 遍历temp对象，将当前子节点与父节点建立连接
    for (let i in temp) {
        // 判断是否是根节点下的项
        if (temp[i][parent_id] !== 'root') {
            if (!temp[temp[i][parent_id]].children) {
                temp[temp[i][parent_id]].children = new Array();
            }
            temp[temp[i][parent_id]].children.push(temp[i]);
        } else {
            tree[temp[i][id]] = temp[i];
        }
    }

    /*  上面也可以写成
    for(let i=0;i<array.length;i++) {
        temp[array[i][id]] = array[i];
    }
    */

    console.timeEnd('123');
    return tree;
}
// 只取最后一级
export function lookForAllIdLast(data = [], arr = []) {
    for (let index = 0; index < data.length; index++) {
        // data[index].disabled = true//禁止选中
        const item = data[index];

        if (!item.children) {
            arr.push(item.id);
        }
        if (item.children) {
            lookForAllIdLast(item.children, arr);
        }
    }

    return arr;
}
//   取出id
export function lookForAllId(data = [], arr = []) {
    for (let item of data) {
        if (item.parentId !== 0) {
            arr.push(item.id);
        }
        if (item.children && item.children.length)
            lookForAllId(item.children, arr);
    }
    return arr;
}
//   取出id
export function lookForAllIdPid(data = [], arr = []) {
    for (let item of data) {
        // if (item.parentId !== 0) {
        arr.push(item.id);
        // }
        if (item.children && item.children.length)
            lookForAllId(item.children, arr);
    }
    return arr;
}
export function lookForLastIdPid(data = [], arr = []) {
    for (let item of data) {
        if (!item.children) {
            arr.push(item.id);
        }
        if (item.children && item.children.length)
            lookForAllId(item.children, arr);
    }
    return arr;
}
export function filter(nodes, isCheck) {
    let cloneNodes = _.cloneDeep(nodes);
    // 条件就是节点的title过滤关键字
    let predicate = function (node) {
        if (node.checked == isCheck) {
            return true;
        } else {
            return false;
        }
    };
    if (!(cloneNodes && cloneNodes.length)) {
        return [];
    }
    let newChildren = [];
    for (let node of cloneNodes) {
        // 以下只需要考虑自身的节点满足条件即可,不用带上父节点
        let subs = filter(node.children, isCheck);
        if (predicate(node)) {
            newChildren.push(node);
        } else if (subs && subs.length) {
            node.children = subs;
            newChildren.push(node);
        }
        //   if (predicate(node)) {
        //     newChildren.push(node);
        //     node.children = filter(node.children);
        //   } else {
        //     newChildren.push(...filter(node.children));
        //   }
    }
    return newChildren.length ? newChildren : [];
}